package com.wk.controller.teacher;

import com.wk.pojo.Class;
import com.wk.pojo.User;
import com.wk.pojo.User_image_dto;
import com.wk.service.Userinfoservice;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @program: thesis
 * @description: 教师的登录提交功能
 * @author: 王侃
 * @create: 2020-02-14 14:35
 **/
@Controller
public class Tealogincontroller {
    @Autowired
    Userinfoservice userinfoservice;

    //接受Date类型数据
    @InitBinder
    protected void init(HttpServletRequest request, ServletRequestDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    }

    //跳转到登录界面
    @RequestMapping("/teacherlogin")
    public String login()
    {
        return "teacher/login";
    }

    //登录成功跳转到主界面
    @RequestMapping("/tea/main")
    public String main()
    {
        return "teacher/main";
    }

    //登录提交
    @RequestMapping("/tea_loginsubmit")
    public String loginsubmit(String username, String password, Model model)
    {


        //获取当前用户subject
        Subject subject= SecurityUtils.getSubject();
        //进行Md5加密，第一个参数为密码，第二参数是以用户名为盐，第三个参数是加密的次数
        Md5Hash md5registepassword=new Md5Hash( password, username,5);
        //封装用户的登录数据
        UsernamePasswordToken token=new UsernamePasswordToken(username,md5registepassword.toString());

        try {
            subject.login(token);
            return "redirect:/tea/main";

        }catch (UnknownAccountException e)//用户名不存在
        {
            model.addAttribute("msg","用户名不存在");
            return "teacher/login";
        }catch (IncorrectCredentialsException e)
        {
            model.addAttribute("msg","密码错误");
            return "teacher/login";
        }
    }

    //显示教师桌面
    @RequestMapping("/tea/center/desktop")
    public String center_desktop()
    {
        return "teacher/center_desktop";
    }

    //跳转到注册页面
    @RequestMapping("/teacherregister")
    public String register()
    {
        return "teacher/register";
    }

    //返回班级信息
    @ModelAttribute("classtypes")
    public List<Class> classes()
    {
        List<Class> classes=userinfoservice.selectbyallclass();
        return classes;
    }

    //跳转到个人主页
    @RequestMapping("/tea/center/myinformation")
    public String myinformation(HttpSession session,Model model)
    {
        User user=(User)session.getAttribute("loginuser");
        model.addAttribute("user",user);
        return "teacher/myinformation";
    }

    //更新个人信息并且提交
    @RequestMapping("/tea/center/myinformation/updatesubmit")
    public String myinformationupdate(User user,HttpSession session)
    {
        //进行Md5加密，第一个参数为密码，第二参数是以用户名为盐，第三个参数是加密的次数
        Md5Hash md5registepassword=new Md5Hash( user.getPassword(), user.getUsername(),5);
        user.setPassword(md5registepassword.toString());
        userinfoservice.updatebyuser(user);
        //别忘了修改session
        session.setAttribute("loginuser",user);
        return "common/success";
    }

    //跳转到头像
    @RequestMapping("/tea/center/myinformation_icon")
    public String myinformation_icon(HttpSession session,Model model)
    {
        User user=(User)session.getAttribute("loginuser");
        model.addAttribute("user",user);
        return "teacher/myinformation_icon";
    }

    //更新个人头像并且提交
    @RequestMapping("/tea/center/my_icon/updatesubmit")
    public String my_icon(HttpSession session, MultipartFile icon) throws IOException
    {
        User user=(User)session.getAttribute("loginuser");
        //将头像储存路径放置在C盘的icon文件夹中
        String path="C:\\icon";
        User_image_dto dto=new User_image_dto();
        //先将数据放入dto
        dto.setUser_id(user.getId());
        dto.setI_uploadpath(path);
        dto.setI_filename(icon.getOriginalFilename());
        dto.setI_inputStream(icon.getInputStream());
        userinfoservice.updatemyicon(dto);
        //最后在service类中，把dto交给服务类处理
        return "common/success";
    }

    //核查修改的名称和别人的是否相同
    @RequestMapping(value = "/tea/checkmyname",method= RequestMethod.POST,consumes="application/json")
    @ResponseBody
    public boolean checkusername(@RequestBody User user, HttpSession session)
    {
        User myuser=(User)session.getAttribute("loginuser");

        String username=user.getUsername();
        User user1=userinfoservice.selectbyusername(username);

        if (user1==null||myuser.getUsername().equals(user.getUsername()))
            return true;
        else
            return false;
    }

    //教师退出登录
    @RequestMapping("/tea/logout")
    public String logout(HttpSession session)
    {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "student/login";
    }
}
